<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Http\Controllers\Controller;
use App\Models\CourseSectionResources;
use App\Models\CourseSectionVideo;
use App\Services\CourseSectionResourcesService;
use App\Services\CourseSectionVideoService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Vod\Model\VodUploadRequest;

require '../vendor/autoload.php';
use Vod\VodUploadClient;



class CourseSectionVideoController extends AdminController
{
    //添加
    public function  add(Request $request){
        $validator = Validator::make($request->all(), [
            'course_id' => 'required|integer|digits_between:1,20|min:1',
            'course_section_id' => 'required|integer|digits_between:1,20|min:1',
            'name' => 'required|string',
            'media_url' => 'required|string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        CourseSectionVideo::create($request->all());
        return $this->success();
    }

    //修改
    public function  update(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
            'course_id' => 'required|integer|digits_between:1,20|min:1',
            'course_section_id' => 'required|integer|digits_between:1,20|min:1',
            'name' => 'required|string',
            'media_url' => 'required|string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $putData = $request->except(['_method','_token']);
        $courseSectionVideo = new CourseSectionVideo();
        $courseSectionVideo->find($request->id)->update($putData);
        return $this->success();
    }
    //列表
    public function  list(Request $request){
        $validator = Validator::make($request->all(), [
            'sort' => 'string',
            'order' => 'string',
            'course_id' => 'integer',
            'course_section_id' => 'integer',
            'name' => 'string',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }

        $sort = $request->input('sort','created_at');
        $order = $request->input('order','asc');
        $name = $request->input('name');
        $course_id = $request->input('course_id');
        $course_section_id = $request->input('course_section_id');

        $list = CourseSectionVideoService::getInstance()->list($sort,$order,$course_id, $course_section_id, $name);
        return $this->success($list);
    }

    //删除
    public function  delete(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $courseSectionVideo = CourseSectionVideo::find($request->id);
        $courseSectionVideo->delete();
        return $this->success();
    }
    public function vodFile(Request $request){
        // 上传到服务器
        $url = config('filesystems.disks.course.url');
        $default = config('filesystems.disks.course.default');
        $temp_file =  CourseSectionVideoService::getInstance()->upfile($request,$url,$default,'course');
        $temp_file =  public_path().$temp_file;


        // 服务器上传到云点播
        $client = new VodUploadClient("AKIDzpQmumPmzwe7KI1UihQuBD0YruiGYaNu", "JNBkKfkbCXeW9WdV7UbS4RZSCk9OSM7j");
        $req = new VodUploadRequest();

        $req->Procedure = "meikeyouxue"; //指定任务流
        $req->CoverFilePath ="";//封面
        $req->MediaFilePath = $temp_file; //地址

        $rsp = $client->upload("ap-beijing", $req);
//        dd($rsp);
        return $this->success($rsp);
    }
    public function getAutograph(){
        // 确定 App 的云 API 密钥
        $secret_id = "AKIDzpQmumPmzwe7KI1UihQuBD0YruiGYaNu";
        $secret_key = "JNBkKfkbCXeW9WdV7UbS4RZSCk9OSM7j";
        // 确定签名的当前时间和失效时间

        $current = time();
        $expired = $current + 86400;  // 签名有效期：1天
        // 向参数列表填入参数
        $arg_list = array(
            "secretId" => $secret_id,
            "currentTimeStamp" => $current,
            "expireTime" => $expired,
            "random" => rand());
        // 计算签名
        $original = http_build_query($arg_list);
        $signature = base64_encode(hash_hmac('SHA1', $original, $secret_key, true).$original);
        return $signature;
    }
}
